package model

import (
	"errors"
	"github.com/jinzhu/gorm"
)

type ResultInfo struct {
	gorm.Model
	AlgorithmID      int64  `json:"algorithm_id"`
	ResultStatistics string `json:"result_statistics"`
	JobID            string `json:"job_id"`
	imagePath        string `json:"image_path"`
}

// AddNewResult add a new Result
func AddNewResult(resultInfo *ResultInfo) (interface{}, error) {
	result := db.Save(resultInfo)
	return result.Value, nil
}

// GetResult get a single Result
func GetResult(ResultID string) (*ResultInfo, error) {
	var Result ResultInfo
	// SELECT * FROM Result WHERE id = ResultID;
	db.First(&Result, ResultID)
	if Result.ID == 0 {
		err := errors.New("no Result found")
		return &ResultInfo{}, err
	}
	return &Result, nil
}

// GetResultWithJobID get a single Result
func GetResultWithJobID(userID string) ([]ResultInfo, error) {
	var Result []ResultInfo

	db.Where("job_id = ?", userID).Find(&Result)

	if len(Result) == 0 {
		err := errors.New("no Result found")
		return []ResultInfo{}, err
	}

	return Result, nil
}
